package com.wltjack.springboot.controller;

import com.google.gson.Gson;
import com.wltjack.springboot.dto.UserDto;
import com.wltjack.springboot.service.UserService;
import com.wltjack.springboot.vo.UserVO;
import com.wltjack.springboot.vo.common.PageDto;
import com.wltjack.springboot.vo.common.ResultVO;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UserService userService;
	@Autowired
	private Gson gson;
	
	/**
	 * 会员列表页面
	 * <p></p>
	 * http://127.0.0.1:8080/learn/user/index
	 */
	@RequestMapping(value = "/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/app/user.html").forward(request, response);
	}
	
	@ApiOperation(value="获取用户列表", notes="获取用户列表")
	@RequestMapping(value = "/getAllUsers", method = RequestMethod.POST)
	public ResultVO getAllUsers(UserDto userDto){
		PageDto allUsers = userService.getAllUsers(userDto);
		return new ResultVO(allUsers);
	}
	
	// http://127.0.0.1:8080/learn/user/detail/2
	@ApiOperation(value="根据id查询某个用户", notes="根据id查询某个用户")
	@RequestMapping(value="detail/{id}", method = RequestMethod.GET)
	public UserVO getUserDetailById(@PathVariable Integer id) {
		return userService.getUserDetailById(id);
	}
	
	@ApiOperation(value="新增用户", notes="新增用户")
	@RequestMapping(value="/addUser", method = RequestMethod.POST)
	public String addUser(
			@RequestParam(value = "userName", required = true) String userName,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "address", required = false) String address
			) {
		UserVO userVO = new UserVO();
		userVO.setUserName(userName);
		userVO.setName(name);
		userVO.setAddress(address);
		return userService.addUser(userVO);
	}
	
	@ApiOperation(value="修改用户", notes="修改用户")
	@RequestMapping(value="/editUser", method = RequestMethod.POST)
	public String editUser(
//			@RequestParam(value = "id", required = true) Long id,
//			@RequestParam(value = "userName", required = true) String userName,
//			@RequestParam(value = "name", required = true) String name,
//			@RequestParam(value = "address", required = false) String address
			@RequestBody UserVO userVO
			) {
//		UserVO userVO = new UserVO();
//		userVO.setId(id);
//		userVO.setUserName(userName);
//		userVO.setName(name);
//		userVO.setAddress(address);
		log.info("/user/editUser > params userVO = {}" ,gson.toJson(userVO));
		return userService.updateUser(userVO);
	}
	
	@ApiOperation(value="根据id删除某个用户", notes="根据id删除某个用户")
	@RequestMapping(value="/delete/{id}", method = RequestMethod.POST)
	public String delete(@PathVariable Integer id) {
		return userService.deleteUserById(id);
	}
	
	
	
	
}
